package org.dbstar.scheduler.actions;

import java.util.ArrayList;
import java.util.List;

import org.dbstar.scheduler.beans.Catalog;
import org.dbstar.scheduler.beans.CompoundJobConf;
import org.dbstar.scheduler.beans.JobConf;
import org.dbstar.scheduler.beans.JobDefine;
import org.dbstar.scheduler.beans.JobDependency;
import org.dbstar.scheduler.beans.LoopJobConf;
import org.dbstar.scheduler.beans.OperateType;
import org.dbstar.scheduler.control.CatalogController;
import org.dbstar.scheduler.control.JobConfController;
import org.dbstar.scheduler.control.JobDependencyController;
import org.dbstar.scheduler.control.exception.DaoException;
import org.dbstar.scheduler.identifier.DefaultIdentifier;
import org.dbstar.scheduler.identifier.Identifier;
import org.dbstar.scheduler.utils.Utils;
import org.quartz.Scheduler;


/**
 * 查看JobConf信息
 * 
 * @author 代波
 * @since 1.0.0
 * @version 1.0.0
 * @date 2012-4-8
 */
public class JobConfsAction extends ActionSupport {
	// 仅接收
	private JobConfController jobConfController;
	private JobDependencyController jobDependencyController;
	private Scheduler scheduler;
	private String change;
	private Identifier identifiers[];
	private boolean noUseOnly = false;

	// 仅传出
	private List<Catalog> catalogs;
	private List<JobConf> confs;

	// 接收并传出
	private CatalogController catalogController;
	private int catalog;// 当前选中的Catalog
	private int targetCatalog;// 移动Conf的目标Catalog
	private String message;

	// 仅接收
	public void setJobConfController(JobConfController jobConfController) {
		this.jobConfController = jobConfController;
	}

	public void setJobDependencyController(JobDependencyController jobDependencyController) {
		this.jobDependencyController = jobDependencyController;
	}

	public void setScheduler(Scheduler scheduler) {
		this.scheduler = scheduler;
	}

	public void setChange(String change) {
		this.change = change;
	}

	public void setIdentifiers(Identifier[] identifiers) {
		this.identifiers = identifiers;
	}

	public void setNoUseOnly(boolean noUseOnly) {
		this.noUseOnly = noUseOnly;
	}

	// 仅传出
	public List<Catalog> getCatalogs() {
		return catalogs;
	}

	public List<JobConf> getConfs() {
		return confs;
	}

	// 接收并传出
	public CatalogController getCatalogController() {
		return catalogController;
	}

	public void setCatalogController(CatalogController catalogController) {
		this.catalogController = catalogController;
	}

	public int getCatalog() {
		return catalog;
	}

	public void setCatalog(int catalog) {
		this.catalog = catalog;
	}

	public int getTargetCatalog() {
		return targetCatalog;
	}

	public void setTargetCatalog(int targetCatalog) {
		this.targetCatalog = targetCatalog;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@Override
	public String execute() throws Exception {
		try {
			if ("changeCatalog".equals(change)) {
				if (identifiers != null && identifiers.length > 0) {
					if (targetCatalog > 0) {
						for (Identifier id : identifiers) {
							jobConfController.moveJobConf(id, targetCatalog);
							operate(OperateType.conf_move, id);
						}
					} else {
						message = "请选择需要移动的目标分类。";
					}
				} else {
					message = "请选择需要移动的Job定义。";
				}
			}
		} catch (DaoException e) {
			message = e.getLocalizedMessage();
		}

		catalogs = Utils.getAllCatalogs(catalogController);
		if (catalog > 0) {
			confs = jobConfController.getJobConfs(catalog);
		} else {
			confs = jobConfController.getAllJobConfs();
		}

		if (noUseOnly) {
			// 去掉所有被依赖触发的
			for (JobDependency dependency : jobDependencyController.getAllJobDependencies()) {
				confs.remove(jobConfController.getJobConf(dependency.getJobConfIdentifier()));
			}
			// 去掉所有被调度的
			for (String id : scheduler.getTriggerGroupNames()) {
				confs.remove(jobConfController.getJobConf(DefaultIdentifier.valueOf(id)));
			}
			// 去掉所有被其他复合任务引用的
			List<JobConf> noUseConfs = new ArrayList<JobConf>(confs);
			for (JobConf c : jobConfController.getAllJobConfs()) {
				for (JobConf uc : confs) {
					if (containJob(c, uc.getIdentifier())) {
						noUseConfs.remove(uc);
					}
				}
			}
			confs = noUseConfs;
		}

		operate(OperateType.confs_list, String.valueOf(catalog));

		return SUCCESS;
	}

	private static boolean containJob(JobConf conf, Identifier identifier) {
		if (CompoundJobConf.JOB_TYPE.equals(conf.getType())) {
			for (JobDefine jd : ((CompoundJobConf) conf).getJobs()) {
				if (identifier.equals(jd.getIdentifier())) {
					return true;
				}
			}
		} else if (LoopJobConf.JOB_TYPE.equals(conf.getType())) {
			JobDefine jd = ((LoopJobConf) conf).getSubJob();
			return identifier.equals(jd.getIdentifier());
		}
		return false;
	}

}
